package com.ambulance.project.biz.controller.pad;

import com.ambulance.framework.web.domain.AjaxResult;
import com.ambulance.framework.web.domain.AjaxResultData;
import com.ambulance.project.biz.entity.Dispatch;
import com.ambulance.project.biz.entity.QuickWords;
import com.ambulance.project.biz.service.QuickWordsService;
import com.ambulance.project.manager.utils.DoctorAndNurseCommonUtil;
import com.ambulance.project.manager.utils.PadUserLoginResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;


@Api(tags = {"随车医生-快捷单词相关接口"})
@Controller
@RequestMapping(value = "/pad/v1/words", method = RequestMethod.POST)
public class QuickWordsController {

    @Autowired
    private QuickWordsService quickWordsService;
    @Autowired
    private DoctorAndNurseCommonUtil padCommonUtil;

    @RequestMapping(value = "add")
    @ResponseBody
    @ApiOperation(value = "添加单词", notes = "电子病历-普通病历-现场诊断-加号", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "类型1=既往病史,2=过敏史,3=抢救措施", required = true, paramType = "form", dataType = "int"),
            @ApiImplicitParam(name = "value", value = "值", required = true, paramType = "form", dataType = "String"),
    })
    public Object add(
            @RequestParam(value = "type") Integer type,
            @RequestParam(value = "value") String value
    ) {
        QuickWords quickWords = new QuickWords();
        quickWords.setType(type);
        quickWords.setValue(value);
        Dispatch dispatch = padCommonUtil.getDispatch();
        PadUserLoginResult padLoginUser = padCommonUtil.getPadLoginUser();
        //加数据
        if ("DOCTOR".equals(padLoginUser.getRole())) {
            quickWords.setGenre("1");
            quickWords.setUserid(dispatch.getDoctorID().toString());
        } else {
            quickWords.setGenre("2");
            quickWords.setUserid(dispatch.getNurseID().toString());
        }

        boolean save = quickWordsService.save(quickWords);
        return AjaxResult.AutoResponse(save);
    }

    //    @RequestMapping(value = "update")
//    @ResponseBody
//    @ApiOperation(value = "更新快捷单词", notes = "更新快捷单词", produces = "application/json")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "编号", required = true, paramType = "form", dataType = "int"),
//            @ApiImplicitParam(name = "type", value = "类型", required = false, paramType = "form", dataType = "int"),
//            @ApiImplicitParam(name = "value", value = "值", required = false, paramType = "form", dataType = "String"),
//    })
//    @ApiResponses({
//            @ApiResponse(code = 200, response = AjaxResultData.class, message = "响应")
//    })
    public Object update(
            @RequestParam(value = "id") Integer id,
            @RequestParam(value = "type", required = false) Integer type,
            @RequestParam(value = "value", required = false) String value
    ) {
        QuickWords quickWords = new QuickWords();
        quickWords.setId(id);
        quickWords.setType(type);
        quickWords.setValue(value);
        boolean b = quickWordsService.updateById(quickWords);
        return AjaxResult.AutoResponse(b);
    }

    @RequestMapping(value = "autoInfrequency")
    @ResponseBody
    @ApiOperation(value = "自增快捷单词频率", notes = "用户每点击一次快捷单词都调用这个方法(需要控制一下频率,连续相同的就不需要调这个方法了)", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "编号", required = true, paramType = "form", dataType = "int"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = AjaxResultData.class, message = "响应")
    })
    public Object autoInfrequency(
            @RequestParam(value = "id") Integer id
    ) {
        QuickWords quickWords = new QuickWords();
        quickWords.setId(id);
        final QuickWords quickWordsServiceById = quickWordsService.getById(id);
        quickWords.setFrequency(quickWordsServiceById.getFrequency() + 1);
        boolean b = quickWordsService.updateById(quickWords);
        return AjaxResult.AutoResponse(b);
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    @ApiOperation(value = "快捷单词列表", notes = "电子病历-普通病历(既往病史等快捷点击)", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pNum", value = "第几页", required = false, dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "pSize", value = "每页多少", required = false, dataType = "int", defaultValue = "10"),
    })
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数不正确"),
            @ApiResponse(code = 404, message = "404请求路径不正确"),
            @ApiResponse(code = 200, response = QuickWords.class, message = "响应")
    })
    public Object list(
            @RequestParam(value = "pNum", required = false, defaultValue = "1") Integer pNum,
            @RequestParam(value = "pSize", required = false, defaultValue = "10") Integer pSize
    ) {
        // 获取用户信息
        PadUserLoginResult padLoginUser = padCommonUtil.getPadLoginUser();
        Integer userMark = padLoginUser.getUserMark();
        Integer genre = 2;
        if ("DOCTOR".equals(padLoginUser.getRole())) {
            genre = 1;
        }
        HashMap<String, Object> map = Maps.newHashMap();
        Page<QuickWords> page = new Page<>(pNum, pSize);
        QueryWrapper<QuickWords> queryWrapper = new QueryWrapper<>();
        //查询既往病史快捷单词
        queryWrapper.eq("type", "1").eq("userid", userMark).eq("genre", genre).orderByDesc("frequency");
        IPage<QuickWords> treatmentRecord = quickWordsService.page(page, queryWrapper);
        map.put("treatmentRecord", treatmentRecord.getRecords());
        //查询过敏史快捷单词
        QueryWrapper<QuickWords> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("type", "2").eq("userid", userMark).eq("genre", genre).orderByDesc("frequency");
        IPage<QuickWords> medicineSensive = quickWordsService.page(page, queryWrapper1);
        map.put("medicineSensive", medicineSensive.getRecords());
        //查询抢救措施快捷单词
        QueryWrapper<QuickWords> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("type", "3").eq("userid", userMark).eq("genre", genre).orderByDesc("frequency");
        IPage<QuickWords> treatMeasure = quickWordsService.page(page, queryWrapper2);
        map.put("treatMeasure", treatMeasure.getRecords());
        return AjaxResult.success(map);
    }
}
